import * as sqlite_opt from "@/utils/sqliteUtil.js";

const thickPhotoTableName = "t_photo";

export const requireThickPhotoByMacnumbAndPosno = (inMacNumb, inPosNo) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `SELECT * `;
		sqlStr += `from ` + thickPhotoTableName + ` where 1 = 1 `;
		sqlStr += inMacNumb ? (` and mac_numb = '` + inMacNumb + `' `) : ` `;
		sqlStr += inPosNo ? (` and pos_no = ` + inPosNo + ` `) : '';

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const addThickPhotoByMacnumbAndPosno = (inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item, index) => {
		valStr += inObj[item] != null ? ('"' + inObj[item] + '"') : null;

		if (keys.length - 1 != index)
			valStr += ","
	})

	return new Promise((resolve, reject) => {
		let sqlStr = `insert into ${thickPhotoTableName} (${keyStr}) values(${valStr})`;

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateThickPhotoByMacnumbAndPosno = (inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	keys.forEach((item , index) => {
		valStr += item + `  =  "`;
		valStr += inObj[item] + `" ` ;
		
		if (keys.length - 1 != index)
			valStr += ","
	})

	return new Promise((resolve, reject) => {		
		let sqlStr = `update ${thickPhotoTableName} set ${valStr} where t_id = "${inObj.t_id}"`;

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}


export const deleteThickPhotoByMacnumbAndPosno = (inTid) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `delete from ${thickPhotoTableName} where t_id = ${inTid} `;

		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}
